package com.type.web;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.ibm.icu.text.SimpleDateFormat;
import com.type.dao.ServiceRequestDAO;
import com.type.service.FileUtil;
import com.type.vo.ServiceRequestVO;
import com.vo.UserVO;

/**
 * 복무신청
 * 파일시스템을 이용하므로 파일의 저장 경로 -> C:\eGovFrameDev2.5.1\springrecipe\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\templateType\img\type\typeOne
 * @author hyem55
 * @since 2013.05.21
 */
@Controller
public class TypeOneController {

	@Resource(name="ServiceRequestDAO")
	private ServiceRequestDAO serviceRequestDAO;
	
	/*
	 * 복무신청 등록화면
	 */
	@RequestMapping("/typeOne.do")
	public String insertServiceRequest(Model model){
		model.addAttribute("serviceRequestVO",new ServiceRequestVO());
		return "/type/typeOne";		
	}
	/*
	 * 복무신청 등록하기
	 */
	@RequestMapping("/typeOneSave.do")
	public String saveServiceRequest(@RequestParam(value = "name") String name, 
			@RequestParam(value = "jongbyul") String jongbyul, 
			@RequestParam(value = "calendarStart") String calendarStart, 
			@RequestParam(value = "calendarEnd") String calendarEnd, 
			@RequestParam(value = "timeStart") String timeStarted, 
			@RequestParam(value = "startMinute") String startedMinute,
			@RequestParam(value = "timeEnd") String timeEnded, 
			@RequestParam(value = "endMinute") String endedMinute,			
			@RequestParam(value = "sau") String sau, 
			@RequestParam(value = "phone") String phone, 
			@RequestParam(value = "bigo") String bigo, 
			@RequestParam(value = "approName") String approName,
			@RequestParam(value = "picture") MultipartFile picture,
			HttpSession session,
			Model model){
		
		int requestNo = serviceRequestDAO.selectSequenceNo();
		String fileName = null;
		if(Integer.parseInt(timeStarted)<10){
			timeStarted = "0"+timeStarted;
		}
		if(Integer.parseInt(timeEnded)<10){
			timeEnded = "0"+timeEnded;
		}
		if(Integer.parseInt(startedMinute)<10){
			startedMinute = "0"+startedMinute;
		}
		if(Integer.parseInt(endedMinute)<10){
			endedMinute = "0"+endedMinute;
		}
		String timeStart = timeStarted + "시 "+startedMinute + "분";
		String timeEnd = timeEnded+ "시 "+endedMinute+ "분";
		
		calendarStart = calendarStart.substring(0, 4) + calendarStart.substring(5,7) + calendarStart.substring(8,10)+timeStarted+startedMinute;
		calendarEnd = calendarEnd.substring(0,4) + calendarEnd.substring(5,7)+ calendarEnd.substring(8,10)+timeEnded+endedMinute;
		
		SimpleDateFormat calStartDate = new SimpleDateFormat("yyyyMMddHHmm");
		Date dateStart  =null;
		Date dateEnd =null;  
		try{
			dateStart = calStartDate.parse(calendarStart);
			dateEnd = calStartDate.parse(calendarEnd);
			
			if(picture.getOriginalFilename()!=""){
				FileUtil.validateFile(picture);
				fileName = FileUtil.getRealFileName(requestNo, session);
				FileUtil.saveFile(fileName, picture);
			}
			ServiceRequestVO serviceRequestVO = new ServiceRequestVO(requestNo, name, jongbyul, dateStart, dateEnd, timeStart, timeEnd, sau, phone, bigo, approName, fileName);
			serviceRequestDAO.insertServiceRequest(serviceRequestVO);
			model.addAttribute("serviceRequestVO",serviceRequestVO);
			
		}catch(Exception e){
			e.printStackTrace();
			model.addAttribute("message",e.getMessage()+"<br> 신청하는데 오류가 발생했습니다.");
			return "forward:/error.do";
		}
		return "redirect:/typeOneList.do";
	}
	
	/*
	 * 신청한 복무기록 전체리스트
	 */
	@RequestMapping("/typeOneList.do")
	public String selectServiceRequestList(Model model){
		model.addAttribute("selectServiceRequestList", serviceRequestDAO.selectServiceRequestList());
		return "/type/typeOne/typeOneList";
	}
	
	/*
	 * 전체리스트에서 선택한 복무신청 기록 상세보기
	 */
	@RequestMapping("/typeOneView.do")
	public String selectServiceRequestViewByNo(Model model,@RequestParam(value="requestNo", required=false)String requestNo){
		ServiceRequestVO serviceRequestVO = serviceRequestDAO.selectServiceRequestViewByNo(Integer.parseInt(requestNo));
		Integer requestNoChange = Integer.parseInt(requestNo);
		String fileName = serviceRequestVO.getFileName();
		
		model.addAttribute("selectServiceRequestViewByNo", serviceRequestVO);
		if(fileName==null){
			model.addAttribute("imageUrl", fileName);
		}else{
			model.addAttribute("imageUrl", FileUtil.getImageUrl(requestNoChange.intValue()));
		}
		
		return "/type/typeOne/typeOneView";
	}
	
	@RequestMapping(value="checkDateForm.do")
	public String checkUsingDateForm(){
		return "/type/typeOne/checkDate";
	}
	
	@RequestMapping(value="checkDate.do", method=RequestMethod.POST)
	public String checkUsingDate(Model model, @RequestParam(value="startDate")String startDate, @RequestParam(value="endDate") String endDate, HttpSession session){
		UserVO user = (UserVO) session.getAttribute("user");
		if(user==null){
			model.addAttribute("message","<br> 로그인 후 접근하세요.");
			return "forward:/error.do";
		}
		model.addAttribute("selectServiceRequestList",serviceRequestDAO.selectServiceRequestListByDate(startDate,endDate,user.getName()));
		return "/type/typeOne/checkDate";
	}
	
}
